<?php
	include_once("king_head.php");
?>
<CENTER>
<H2> Add Product/Pricing</H2>
<p align="left"><font color="#FF0000" size="-3" face="Verdana">**</font><font face="Verdana" size="-3">&quot;The &quot;Product Price&quot; listed includes all tax and shipping costs owed to the distributor; however the Reseller is solely responsible to add in thier own individual state tax % where applicable.  All shipping is added in as a flat rate for services and all items are drop shipped directly to the clients so there is no need to add a shipping cost unless the Reseller needs to accomodate the needs for any additional shipping costs associated with special delievery services or other special shipping costs not handled by the distributor.  All orders are based on deliveries going to Contitnental United States so all other clients should call or email for custom order processing to avoid potential fees and delays in delivery.</font></p>
<form name=frm method=post><input type=hidden name=site_id value="<?php echo $_SESSION['SITE_ID']; ?>">
<TABLE border=1 width='90%'>
	<TR>
		<TH>Display Product</TH>
		<TH><input type="submit" name="product_title" value="Product Title"></TH>
		<TH><input type="submit" name="product_category" value="Product Category"></TH>
		<TH>Product price</TH>
		<TH>Profit (%)</TH>
		<TH>Enable Default Profit (%)</TH>
		<TH>Selling Price</TH>
		<TH>Tax (%)</TH>
		<TH>Enable Default Tax (%)</TH>
		<TH>Shipping Amount</TH>
		<TH>Calculate Shipping</TH>
		<TH>Final Price</TH>
		<TH>Calculator</TH>
		<TH>Product Order for Display</TH>
	</TR>
<?php
	include_once("ClsSebent/config.php");
$order=(isset($_POST["product_title"]))? "product_title" : "product_category";
if($order=="")$order="product_title";
//echo $order;
$action=$_REQUEST['action'];

if(($action=="Save" || $action=="Recalculate") && $_SESSION['SITE_ID'] != "")
	{
		$sql="DELETE FROM site_product WHERE  site_id='".$_SESSION['SITE_ID']."'";
//		echo $sql;

		mysql_query($sql);

		for($i=1;$i<=$num;$i++)
		{	
			${'pro_id_'.$i}=$_REQUEST['pro_id_'.$i];
			${'pft_chk_'.$i}=$_REQUEST['pft_chk_'.$i];
			${'id_'.$i}=$_REQUEST['id_'.$i];
			${'product_title_'.$i}=$_REQUEST['product_title_'.$i];
			${'profit_pct_'.$i}=$_REQUEST['profit_pct_'.$i];
			${'tax_chk_'.$i}=$_REQUEST['tax_chk_'.$i];
			${'price_'.$i}=$_REQUEST['price_'.$i];
			${'tax_pct_'.$i}=$_REQUEST['tax_pct_'.$i];
			${'shipping_amt_'.$i}=$_REQUEST['shipping_amt_'.$i];
			${'final_price_'.$i}=$_REQUEST['final_price_'.$i];
			${'product_position_'.$i}=$_REQUEST['product_position_'.$i];
			if(${'pro_id_'.$i}=="Y")
			{
				if(${'pft_chk_'.$i}!="Y")${'pft_chk_'.$i}='N';
				if(${'tax_chk_'.$i}!="Y")${'tax_chk_'.$i}='N';
				
			$sql="INSERT INTO site_product (product_id, site_id, product_title, profit_pct, profit_chk, price, tax_pct, tax_chk, shipping_amt, final_price, product_position) values(" . ${'id_' . $i} . ", " . $_SESSION['SITE_ID'] . ", '" . ${'product_title_' . $i} . "', " . ${'profit_pct_' . $i} . ", '".${'pft_chk_' . $i}."', " . ${'price_' .$i} . ", " . ${'tax_pct_' . $i} .", '" .${'tax_chk_' . $i}. "', " . ${'shipping_amt_' . $i} . ",".${'final_price_' . $i}.",'".${'product_position_'.$i}."')";
			
		
			mysql_query($sql);
			
			}
		}
	}

	$sql="SELECT * FROM product_master ORDER BY ".$order;

	$result = mysql_query($sql);
	$num=1;
	while($row=mysql_fetch_array($result))
	{
	#	$pro_id=$row["id"];
		$pro_id=$row["product_id"];
		$product_title=$row["product_title"];
		$product_category=$row["product_category"];
		$product_desc=$row["product_desc"];
		$product_img=$row["product_img"];
		$product_img_alt=$row["product_img_alt"];
		$product_price=$row["product_price"];		
		
		$sql="SELECT * FROM site_product WHERE  site_id='".$_SESSION['SITE_ID']."' AND product_id='".$pro_id."'";// ORDER BY ".$order;  
		//echo $sql;
		$site_result = mysql_query($sql);

		if($site_row=mysql_fetch_array($site_result))
		{
			$exist="checked";
			$profit_pct=$site_row["profit_pct"];
			$price=$site_row["price"];
			$tax_pct=$site_row["tax_pct"];
			$shipping_amt=$site_row["shipping_amt"];
			$product_position=$site_row["product_position"];//new
			$pft_chk=$site_row["profit_chk"];
			$tax_chk=$site_row["tax_chk"];
			$final_price=$site_row["final_price"];

				//Default Percentages
				$pctqry="select tax_pct, pro_pct from site_home where id=".$_SESSION['SITE_ID'];
				//echo $pctqry;
				$pctres=mysql_query($pctqry);
				$pctrow=mysql_fetch_array($pctres);
				$dtax=$pctrow['tax_pct'];
				$dprft=$pctrow['pro_pct'];				
		}
		else
		{
			$exist="";
			$profit_pct=0;
			//$price=$product_price * 1.1;
			$tax_pct=0;
			$shipping_amt=0;
						//Default Percentages
				$pctqry="select tax_pct, pro_pct from site_home where id=".$_SESSION['SITE_ID'];
				$pctres=mysql_query($pctqry);
				$pctrow=mysql_fetch_array($pctres);
				$dtax=$pctrow['tax_pct'];
				$dprft=$pctrow['pro_pct'];			
		}			

		if($_SESSION['SITE_ID']!=101)
		{
			$pqry="Select final_price from site_product where site_id=101 and product_id=".$pro_id;
			//echo $pqry;
			$pres=mysql_query($pqry);			
			if($prow=mysql_fetch_array($pres))
			$product_price=$prow["final_price"];
			//echo"Admin final: ".$product_price;
		}		
		
		if($tax_chk=='Y')
		{$atax_pct=$dtax;
		$tchecked="checked";}
		else
		{$atax_pct=$tax_pct;
		$tchecked="";}
		
		if($pft_chk=='Y')
		{$apft_pct=$dprft;
		$pchecked="checked";}
		else
		{$apft_pct=$profit_pct;		
		$pchecked="";}
		
			$pft_price=$product_price*($apft_pct/100);
			$price=$product_price+$pft_price;
			$tax_price=$price*($atax_pct/100);
			$final_price=$product_price+$pft_price+$tax_price+$shipping_amt;
			//echo "final: ".$final_price;		
			if($product_price=="")$product_price=0;
			if($profit_pct=="")$profit_pct=0;
			if($tax_pct=="")$tax_pct=0;
			if($shipping_amt=="")$shipping_amt=0;
			if($final_price=="")$final_price=0;

		echo "<tr>";
		echo "<td><input type=hidden name=id_$num value=$pro_id><input type=checkbox name=pro_id_$num $exist value='Y'></td>";
		echo "<td>" . $product_title . "<input type=hidden name=product_title_$num value='$product_title'></td>";
		echo "<td>" . $product_category . "<input type=hidden name=product_category_$num value='$product_category'></td>";
		echo "<td>" . $product_price . "<input type=hidden name=pro_price_$num value=$product_price></td>";	
		echo "<td><input name=profit_pct_$num value='" . $apft_pct . "' size=5 maxlength=5 </td>";?>		
		<td><input type="checkbox" name="pft_chk_<?php echo $num;?>"  <?php echo $pchecked;?> value='Y' onclick="javascript: pftchange(<?php echo $dprft;?>, profit_pct_<?php echo $num;?>, pft_chk_<?php echo $num;?>)"></td><?php
		echo "<td><input name=price_$num value='" . $price . "' size=8 maxlength=8></td>";
		echo "<td><input name=tax_pct_$num value='" . $atax_pct . "'  size=5 maxlength=5 </td>";?>		
		<td><input type="checkbox" name="tax_chk_<?php echo $num;?>" <?php echo $tchecked;?> value='Y' onclick="javascript: taxchange(<?php echo $dtax;?>, tax_pct_<?php echo $num;?>, tax_chk_<?php echo $num;?>)"></td><?php
		echo "<td><input name=shipping_amt_$num value='" . $shipping_amt . "'  size=8 maxlength=8 </td>";?>
		<td><input type="button" name="calcship<?php echo $num;?>" value="Shipping" onclick="javascript: window.open('ups_ship.php?cont=shipping_amt_<?php echo $num;?>','','target=_blank');" />
		<?php
		echo "<td><input name=final_price_$num value='" . $final_price . "'  size=8 maxlength=8></td>";?>
		<td><input type="button" name="calc<?php echo $num;?>" value="Calculate" onclick="javascript: readycalc(tax_pct_<?php echo $num;?>, profit_pct_<?php echo $num;?>, shipping_amt_<?php echo $num;?>, final_price_<?php echo $num;?>, pro_price_<?php echo $num;?>, price_<?php echo $num;?>);" /></td>
		<?php
		echo "<td><input name=product_position_$num value='" . $product_position . "' size=8 maxlength=50></td>";//new
		echo "</tr>";
		$num=$num+1;		
	}
?>
<input type=hidden name=num value="<?php echo $num; ?>">
</TABLE><BR><input type=submit name=action value="Save"></form>
</CENTER>
<script type="text/javascript">
	function pftchange(profit, pftbox, chkbox)
	{
	if(chkbox.checked)
	{//alert("Hi");
	pftbox.value=profit;}
	else
	pftbox.value=0;
	}
	
	function taxchange(tax, taxbox, chkbox)
	{
	if(chkbox.checked)
	taxbox.value=tax;
	else
	taxbox.value=0;
	}

	function readycalc(taxbox, pftbox, ship, fprc, pro_prc, spr)
	{
		spr.value=parseFloat(parseFloat(pro_prc.value) + parseFloat(pro_prc.value) * parseFloat(pftbox.value)/100);	
		fprc.value=parseFloat(parseFloat(spr.value)+ parseFloat(ship.value) + parseFloat(spr.value) * parseFloat(taxbox.value)/100);
	}
</script>
<?php
	include_once("king_foot.php");
?>